package com.voice.core.connection.frame;

import com.voice.core.connection.Connection;
import com.voice.core.utils.BytesUtil;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class PingFrame extends AbstractFrame{

    public PingFrame(){}
    public PingFrame(int streamId,boolean ack,long data){
        this.length=8;
        this.streamId=streamId;
        this.type=FrameType.ping.getType();
        ack(ack);
        ByteBuffer buffer=ByteBuffer.allocate(length+headerLength);
        buffer.put(BytesUtil.intToBytes(length,3));
        buffer.put(type);
        buffer.put(flag);
        buffer.putInt(streamId);
        buffer.putLong(data);
        payload=buffer.array();
    }

    @Override
    public void onFinish(Connection connection) {
        long data=BytesUtil.getLong(payload,headerLength);
        if(ack()) connection.onPingAckRead(data);
        else connection.onPingRead(data);
    }

}
